package com.shanghaionstar.nav.share;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.map.common.PoiItemInfo;
import com.shanghaionstar.R;
import com.shanghaionstar.share.AbstractShareDialog;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.ShareWXUtil;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;


/**
 * Created by Yonglang Hong on 2016/7/22.
 */
public class SharePoiDialog extends AbstractShareDialog {
    /**
     * 暂时针对poi信息分享
     */
    private PoiItemInfo shareItemInfo;
    public SharePoiDialog(Context context, PoiItemInfo shareItemInfo) {
        super(context);
        this.shareItemInfo = shareItemInfo;
    }



    public SharePoiDialog(Context context, int theme, PoiItemInfo shareItemInfo) {
        super(context, theme);
        this.shareItemInfo = shareItemInfo;
    }

    public SharePoiDialog(Context context, boolean cancelable, OnCancelListener cancelListener, PoiItemInfo shareItemInfo) {
        super(context, cancelable, cancelListener);
        this.shareItemInfo = shareItemInfo;
    }


    //分享到微信好友
    @Override
    public void shareToWX() {
        if (!CommonUtil.checkNetWorkState(context)) {
            Toast.makeText(context, context.getResources().getString(R.string.networkError),
                    Toast.LENGTH_LONG).show();
        }

        if (!weiXinApi.isWXAppInstalled()) {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse("http://weixin.qq.com");
            intent.setData(content_url);
            context.startActivity(intent);
        } else {
            if (null != shareItemInfo) {
//                if (clonePoiItemInfo.getTitle().indexOf("我的位置") >= 0
//                        || clonePoiItemInfo.getTitle().indexOf("Current location") >= 0
//                        || clonePoiItemInfo.getTitle().indexOf("车辆位置") >= 0
//                        || clonePoiItemInfo.getTitle().indexOf("Vehicle location") >= 0) {
//                    clonePoiItemInfo.setSnippet(addressDescription);
//                } else {
//                    if (clonePoiItemInfo.getSnippet() == null
//                            || "".equalsIgnoreCase(clonePoiItemInfo.getSnippet())) {
//                        clonePoiItemInfo.setSnippet("");
//                    }
//                }
                if (shareItemInfo.getTelphone() == null || "".equalsIgnoreCase(shareItemInfo.getTelphone())) {
                    shareItemInfo.setTelphone("");
                }
                new ShareWXUtil().shareToFriend(context, shareItemInfo, weiXinApi, SendMessageToWX.Req.WXSceneSession);
            }
        }
    }

    //微信朋友圈
    @Override
    public void shareToWXF() {
        if (!CommonUtil.checkNetWorkState(context)) {
            Toast.makeText(context, context.getString(R.string.networkError), Toast.LENGTH_LONG)
                    .show();
        }
        if (!weiXinApi.isWXAppInstalled()) {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse("http://weixin.qq.com");
            intent.setData(content_url);
            context.startActivity(intent);
        } else {
            // 检测微信版本 4.2以上的 才支持分享到朋友圈 版本过低 提示用户下载
            if (weiXinApi.getWXAppSupportAPI() < 0x21020001) {
                AlertDialogUtil.showAlertDialog(context, "",
                        context.getString(R.string.weixin_version_too_old));
            } else {
//                if (clonePoiItemInfo.getTitle().indexOf("我的位置") >= 0
//                        || clonePoiItemInfo.getTitle().indexOf("Current location") >= 0
//                        || clonePoiItemInfo.getTitle().indexOf("车辆位置") >= 0
//                        || clonePoiItemInfo.getTitle().indexOf("Vehicle location") >= 0) {
//                    clonePoiItemInfo.setSnippet(addressDescription);
//                } else {
//                    if (clonePoiItemInfo.getSnippet() == null || "".equalsIgnoreCase(clonePoiItemInfo.getSnippet())) {
//                        clonePoiItemInfo.setSnippet("");
//                    }
//                }
                if (shareItemInfo.getTelphone() == null || "".equalsIgnoreCase(shareItemInfo.getTelphone())) {
                    shareItemInfo.setTelphone("");
                }
                new ShareWXUtil().shareToFriend(context, shareItemInfo, weiXinApi,
                        SendMessageToWX.Req.WXSceneTimeline);
            }
        }
    }
}
